package com.health.controller;

import com.health.utils.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(value = "/check")
public class CheckCodeServlet extends HttpServlet {
    /**
     * 验证码图片输出验证码字母获取
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  获取响应的输出流并使用自动生成验证码工具类
        ServletOutputStream os = response.getOutputStream();
        String checkCode = CheckCodeUtil.outputVerifyImage(100, 40, os, 4);
        //  由于需要在两次请求中使用，所以要使用session会话，存储session域中
        HttpSession session = request.getSession();
        //  将验证码存入Session中
        session.setAttribute("checkCode", checkCode);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
